PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Saving and Loading Libraries of Subroutines

So far, you've seen examples of defining and calling subroutines in the same script. This is useful for functions that are repeated more than once in the same script. But you can also write subroutines for generic functions, such as numeric operations, that are useful in many different scripts. To make a subroutine available in any script, save it as a compiled script, and then use the scripting addition command Load Script to make it available in a particular script. You can use this technique to create libraries of subroutines for use in many scripts.

For example, the following script contains three subroutines: areaOfCircle, which returns the area of a circle based on its radius; factorial , which returns the factorial of a number; and min , which returns the smallest number in a list of numbers.

-- This subroutine computes the area of a circle from its radius.
on areaOfCircle from radius
    -- Make sure the parameter is a real number or an integer.
    if class of radius is contained by {integer, real}
        return radius * pi  -- pi is predefined by AppleScript.
    else
        error "The parameter must be a real number or an integer"
    end if
end areaOfCircle


-- This subroutine returns the factorial of a number.
on factorial(x)
    set returnVal to 1
    if x > 1 then
        repeat with n from 2 to x
            set returnVal to returnVal * n
        end repeat
    end if
    return returnVal
end factorial


-- This subroutine returns the smallest number in a list
on min(numberList)
    -- Check for a valid list.
    if class of numberList is not equal to list ¬
            or numberList is equal to {} then ¬
        return numberList
    set minNum to first item in numberList
    -- If more than one item, find the smallest.
    if length of numberList > 1 then
        repeat with curNum in numberList
            if curNum < minNum then set minNum to curNum
        end repeat
    end if
    return minNum as number
end min

To save this script as a compiled script, choose Save As from the Script Editor's File menu and choose Compiled Script from the Kind pop-up menu. Then save the script as a file called Numeric Operations.

After you save the script as a compiled script, use the Load Script scripting addition command to make the subroutines it contains available in the current script. For example, the Load Script command in the following script assigns the compiled script Numeric Operations to the variable numberLib . To call the subroutines in Numeric Operations, use a Tell statement. The Tell statement in the example calls the factorial subroutine. (You must have a compiled script called Numeric Operations in the specified location for this script to work correctly.)

set numberLib to (load script file ¬
    "Hard Disk:Scripts:Numeric Operations")

tell numberLib
    min({77, 905, 50, 6, 3, 111})   --result: 3
    areaOfCircle from 12            --result: 37.699111843078
    factorial(10)                   --result: 3628800
end tell

The Load Script scripting addition command loads the compiled script as a script object. Script objects are user-defined objects that are treated as values by AppleScript; for more information about them, see Script Objects. For more information about the Load Script command, and about the other standard scripting addition commands distributed with AppleScript, see the following website:

www.apple.com/applescript


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)